package handlers

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

type Resp struct {
	Data         interface{} `json:"data"`
	Code         int         `json:"code"`
	ErrorMessage string      `json:"errMessage"`
}

// SendOkResp return code 200 success info.
func SendOkResp(c *gin.Context, data interface{}) {
	c.JSON(http.StatusOK, Resp{
		Code: 0,
		Data: data,
	})

}

// SendErrResp return failed of error message.
func SendErrResp(c *gin.Context, statusCode int, err interface{}) {
	c.JSON(statusCode, Resp{
		Code:         statusCode,
		ErrorMessage: ToString(err),
	})
}

// ToString translate error type to string.
func ToString(err interface{}) string {
	switch err.(type) {
	case string:
		return err.(string)
	case error:
		return err.(error).Error()
	default:
		return fmt.Sprintf("%v", err)
	}
}
